package com.tangmu.rabbits.adapter.mine;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tangmu.rabbits.R;
import com.tangmu.rabbits.bean.MessageBean;
import com.tangmu.rabbits.ui.activity.mine.MessageDetailsActivity;
import com.tangmu.rabbits.widget.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/26.
 */

public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<MessageBean> datas = new ArrayList<>();

    public MessageAdapter(Context context, List<MessageBean> datas) {
        this.mContext = context;
        this.datas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MessageViewHolder(inflater.inflate(R.layout.rv_item_message_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MessageViewHolder viewHolder = (MessageViewHolder) holder;
        final MessageBean messageBean = datas.get(position);
        viewHolder.tvMessageType.setText("" + messageBean.getTitle());
        viewHolder.civMessageHeader.setImageResource(messageBean.getImg());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MessageDetailsActivity.class);
                intent.putExtra("title", messageBean.getTitle());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MessageViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.civ_message_header)
        CircleImageView civMessageHeader;
        @BindView(R.id.tv_message_type)
        TextView tvMessageType;
        @BindView(R.id.tv_message_time)
        TextView tvMessageTime;
        @BindView(R.id.tv_message_content)
        TextView tvMessageContent;

        public MessageViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
